package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.DepositActivity;
import com.huifaapp.mall.activity.LoginActivity;
import com.huifaapp.mall.activity.TmallActivity;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.Index;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/*
        进入生钱页面
 */
public class ShengQianActivity extends AppCompatActivity implements View.OnClickListener {

    /**          */
   private  TextView todayshengqian;
    /**
     * 查看明细
     */
    private TextView mTvChakanmingxi;
    private LinearLayout mLiTixian;
    /**
     * 累计生钱：123
     */
    private TextView mTvLeiji;
    /**
     * 可用余额：123
     */
    private TextView mTvYue;
    /**
     * 8.36
     */
    private TextView mTvNianhualv;
    private LinearLayout mLiNinahualv;
    /**
     * 200.00
     */
    private TextView mTvHuazong;
    private LinearLayout mLiHuaqianzongshu;
    /**
     * 4
     */
    private TextView mTvHuaci;
    private LinearLayout mLiHuanqinabishu;
    /**
     * 马上去晒
     */
    private Button mButQushai;
    /**
     * 马上分享
     */
    private Button mBut2Fenxiang;
    /**
     * 马上去炫
     */
    private Button mBut3Quxuna;
    /**
     * 马上转发
     */
    private Button mBut4Zhuanfa;
    private ImageView imageView;
    private WebView webView;
    private ImageView imageViewnback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sheng_qian);
        initView();
        getNetLIvingMOney();
        //设置在客户端打开
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;// 返回false
            }
        });
        webView.loadUrl("http://develop.jhjvip.cn:8080/mall/mobile/index/initClintMsgs");
    }
        /*
                网络请求
         */
    private void getNetLIvingMOney() {
        String url = ApiManager.Index+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(this);
        Log.e("shengqianshou","主题url===="+url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Index index = new Gson().fromJson(responseInfo.result, Index.class);
                if (index.getCode().equals("10000")){
                    Index.DataBean data = index.getData();
                    Log.e("datashouye",data.toString());
                    mTvHuaci.setText(data.getConsumeCount()+"");
                    mTvLeiji.setText("累计生钱"+data.getTotalMoney());
                    mTvHuazong.setText(data.getConsume()+"");
                    mTvNianhualv.setText(data.getYearRate()+"");
                    mTvYue.setText("可用余额"+data.getBalance());
                    todayshengqian.setText(data.getTodayMoney()+"");
                }else {
                    Toast.makeText(ShengQianActivity.this,index.getMsg(),Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(ShengQianActivity.this,s,Toast.LENGTH_SHORT).show();

            }
        });


    }


    private void initView() {
        webView = (WebView) findViewById(R.id.web);
        imageViewnback = (ImageView) findViewById(R.id.backsheq);
        imageViewnback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        todayshengqian = (TextView) findViewById(R.id.tv_today_shengqian);
        mTvChakanmingxi = (TextView) findViewById(R.id.tv_Chakanmingxi);
        mTvChakanmingxi.setOnClickListener(this);
        mLiTixian = (LinearLayout) findViewById(R.id.li_tixian);
        mLiTixian.setOnClickListener(this);
        mTvLeiji = (TextView) findViewById(R.id.tv_leiji);
        mTvYue = (TextView) findViewById(R.id.tv_yue);
        mTvNianhualv = (TextView) findViewById(R.id.tv_nianhualv);
        mLiNinahualv = (LinearLayout) findViewById(R.id.Li_ninahualv);
        mLiNinahualv.setOnClickListener(this);
        mTvHuazong = (TextView) findViewById(R.id.tv_huazong);
        mLiHuaqianzongshu = (LinearLayout) findViewById(R.id.li_huaqianzongshu);
        mLiHuaqianzongshu.setOnClickListener(this);
        mTvHuaci = (TextView) findViewById(R.id.tv_huaci);
        mLiHuanqinabishu = (LinearLayout) findViewById(R.id.li_huanqinabishu);
        mLiHuanqinabishu.setOnClickListener(this);
        mButQushai = (Button) findViewById(R.id.but_qushai);
        mButQushai.setOnClickListener(this);
        mBut2Fenxiang = (Button) findViewById(R.id.but2_fenxiang);
        mBut2Fenxiang.setOnClickListener(this);
        mBut3Quxuna = (Button) findViewById(R.id.but3_quxuna);
        mBut3Quxuna.setOnClickListener(this);
        mBut4Zhuanfa = (Button) findViewById(R.id.but4_zhuanfa);
        mBut4Zhuanfa.setOnClickListener(this);
        imageView = (ImageView) findViewById(R.id.imageview);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
                //去晒
            case R.id.but_qushai:
                Intent intent = new Intent(this, SaishouyiActivity.class);
                startActivity(intent);
                break;
                //分享
            case R.id.but2_fenxiang:
                Intent intent2 = new Intent(this, FenxiangActivity.class);
                startActivity(intent2);

                break;
                //炫一下
            case R.id.but3_quxuna:
                Intent intent3 = new Intent(this, Xuan_Yi_XiaActivity.class);
                startActivity(intent3);
                break;
                //转发
            case R.id.but4_zhuanfa:
                Intent intent4 = new Intent(this, Zhuang_FaActivity.class);
                startActivity(intent4);
                break;
                //查看明细
            case R.id.tv_Chakanmingxi:
                Intent intent5 = new Intent(this, MingXiActivity.class);
                startActivity(intent5);
                break;

            case  R.id.li_tixian:
                //跳转提现页面
                Intent intent6 = new Intent(this, DepositActivity.class);
                startActivity(intent6);
                break;
                    //查看明细
            case  R.id.li_huaqianzongshu:
                Intent intent7 = new Intent(this, MingXiActivity.class);
                startActivity(intent7);
                break;
                //查看明细
            case R.id.li_huanqinabishu:
                Intent intent8 = new Intent(this, MingXiActivity.class);
                startActivity(intent8);
                break;
                //年化率
            case  R.id.Li_ninahualv:
                Intent intent9= new Intent(this, NianHuaLvActivity.class);
                startActivity(intent9);
               // TmallActivity
                break;
            case  R.id.imageview:  //跳转京东
                Intent intent10= new Intent(this, TmallActivity.class);
                startActivity(intent10);
                finish();
                break;
        }
    }
        //判断是否登陆；
    public static boolean isLogin(Context context, boolean isNeedToLoginView){
        String token =  SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN);

        if (!TextUtils.isEmpty(token)){
            return true;
        }
        if (isNeedToLoginView) {
            Intent intent = new Intent(context,LoginActivity.class);
            context.startActivity(intent);
            return false;
        }
        return false;
    }
}
